﻿using System;
using System.Linq;
using BLL;
using DAL_NhaSach;

namespace BLL_NhaSach
{
	public class NhaCungCapBLL : IProcessData
	{
		QLNhaSachEntities db;

		public NhaCungCapBLL(string cnStr)
		{
			db = new QLNhaSachEntities(cnStr);
		}

		public object getAllData()
		{
			return (from ncc in db.NhaCungCaps select new { ncc.MaNCC, ncc.TenNCC, ncc.DiaChi, ncc.Email, ncc.SoDienThoai });
		}

		public object findObjects(string find_content)
		{
			throw new NotImplementedException();
		}

		public object findObjects(int find_content)
		{
			return (db.NhaCungCaps.SingleOrDefault(p => p.MaNCC == find_content));
		}

		public object sort(string Col_Name, bool state)
		{
			throw new NotImplementedException();
		}

		public bool addObject(object addObj)
		{
			NhaCungCap ncc = (NhaCungCap)addObj;
			if (ncc != null)
			{
				try
				{
					db.NhaCungCaps.AddObject(ncc);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool deleteObject(int id)
		{
			NhaCungCap ncc = db.NhaCungCaps.SingleOrDefault(p => p.MaNCC == id);
			if (ncc != null)
			{
				try
				{
					db.NhaCungCaps.DeleteObject(ncc);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool updateObject(object updateObj)
		{
			NhaCungCap ncc_new = (NhaCungCap)updateObj;
			NhaCungCap ncc_old = db.NhaCungCaps.SingleOrDefault(p => p.MaNCC == ncc_new.MaNCC);
			if (ncc_old != null)
			{
				try
				{
					ncc_old.TenNCC = ncc_new.TenNCC;
					ncc_old.DiaChi = ncc_new.DiaChi;
					ncc_old.SoDienThoai = ncc_new.SoDienThoai;
					ncc_old.Email = ncc_new.Email;
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}

			}
			else return false;
		}
	}
}